/**
 * 扩展板上的四个独立按键:PC0,PC1,PC2,PC3
 */
#include "single_btn_driver.h"
#include "gd32f4xx.h"
#include "systick.h"
#include "led_driver.h"

static uint32_t SINGLE_BTNS[4] = {SINGLE_BTN1_PIN, SINGLE_BTN2_PIN, SINGLE_BTN3_PIN, SINGLE_BTN4_PIN};

void single_btn_driver_config(void)
{
    // 外设时钟使能:PC
    rcu_periph_clock_enable(RCU_GPIOC);

    // 输入模式,上拉模式,默认
    size_t i;
    for ( i = 0; i < 4; i++)
    {
        gpio_mode_set(GPIOC, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, SINGLE_BTNS[i]);
    }
}

uint8_t single_btn_driver_get_all_status(void)
{
    uint8_t btn_status = 0;
    for (size_t i = 0; i < 4; i++)
    {
        // 默认高电平,按钮按下,变为低电平
        if (gpio_input_bit_get(GPIOC, SINGLE_BTNS[i]) == 0)
        {
            btn_status |= (1 << i);
        }
    }
    return btn_status;
}

bool single_btn_driver_get_status(uint8_t btn_num)
{
    if (btn_num > 3)
    {
        return false;
    }
    return (gpio_input_bit_get(GPIOC, SINGLE_BTNS[btn_num]) == 0);
}

bool single_btn_driver_get_btn1_status()
{
    return single_btn_driver_get_status(0);
}

bool single_btn_driver_get_btn2_status()
{
    return single_btn_driver_get_status(1);
}

bool single_btn_driver_get_btn3_status()
{
    return single_btn_driver_get_status(2);
}

bool single_btn_driver_get_btn4_status()
{
    return single_btn_driver_get_status(3);
}

void single_btn_driver_test(void)
{
    uint8_t status = single_btn_driver_get_all_status();
    led_driver_set(status);
}
